/**
 * Created by smp on 2017/5/2.
 */
var isSearchBarExpand = false;
var index;

var modelPath=ROOT_PATH+'/majorHazard/informationRegistration/';
$(function () {
    $('#belongsUnit').combotree({
        url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0'
    });
    var getSelectedUser = function (data) {
        var id=data[0].id;
        var realName=data[0].realName;
        /*for(var i=1;i<data.length;i++){
         id=id+','+data[i].id;
         realName=realName+','+data[i].realName;
         }*/
        $("#contactContractor").val(id);
        $("#contactContractorName").textbox("setValue",realName);

    };

    $('#contactContractorName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                WindowManager.openLayer({
                    type: 2,
                    title: '添加承包人',
                    area: ['90%', '70%'],
                    content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                    callBack:getSelectedUser
                });
            }
        }],
        editable: false
    });
    //初始化加载datagrid
    $('#dg1').datagrid({
        method: 'post',
        url: ROOT_PATH + '/majorHazard/informationRegistration/list.shtml',
        queryParams: {
            sort: 'addTime',
            order: 'desc'
        },
        // idField: 'id',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect:true,
        sortOrder: true,
        striped: true,
        fit: true,
        columns: [[
            {field: 'id', width: 80, checkbox: true},
            {field: 'devicePartName', title: '关键装置/要害部位名称', width: 100,  sortable: true},
            {field: 'belongsUnitName', title: '单位名称', width: 80,  sortable: true},
            {field: 'level', title: '级别', width: 80, sortable: true},
            {field: 'contactContractorName', title: '联系(承包)人', width: 100 }
        ]],
        toolbar: '#toolbar'
    });

    //选择按钮事件
    $('#btn_select').on('click',function () {

        var checkedRows=$('#dg1').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要编辑的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        WindowManager.closeLayer({
            returnValue: checkedRows
        });
    });
    /**
     * 高级查询列表
     */
    $('#btn_advanced_search').on("click", function () {
        $('#tb2').toggle();
        $('#dg1').datagrid('resize');
    });
    /**
     * 查询按钮
     */
    $('#selectMessage').on('click',function(){
        var devicePartName=$('#devicePartName').textbox("getValue");
        var belongsUnit=$('#belongsUnit').textbox("getValue");
        var contactContractorName=$('#contactContractorName').textbox("getValue");
        var level=$('#level').textbox("getValue");
        var year=$('#year').textbox("getValue");
        $('#dg1').datagrid('load', {devicePartName:devicePartName,belongsUnit:belongsUnit,contactContractorName:contactContractorName,level:level,year:year,sort: 'addTime', order: 'desc'});
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
        $('#tb2').form('reset');
    });
});